package com.hk.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hk.mapper.BusinessMapper;
import com.hk.model.dto.BusinessPageQueryDTO;
import com.hk.model.dto.RequestData;
import com.hk.model.pojo.Business;
import com.hk.model.pojo.Customer;
import com.hk.model.vo.BusinessPageQueryVO;
import com.hk.result.PageResult;
import com.hk.service.BusinessService;
import com.hk.service.CustomerService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
@Slf4j
public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business> implements BusinessService {
    public final CustomerService customerService;

    /**
     * 商机分页查询，条件查询
     * @param businessPageQueryDTO
     * @return
     */
    @Override
    public PageResult getPage(BusinessPageQueryDTO businessPageQueryDTO) {
        // 创建VO，用于装配数据

        List<BusinessPageQueryVO> VOList = new ArrayList<>();

        // 查询条件，模糊搜索，如果有客户ID,手机号，或姓名，则依照客户ID，手机号，或姓名进行模糊搜索
        LambdaQueryWrapper<Customer> customerWrapper = new LambdaQueryWrapper<>();
        if (businessPageQueryDTO.getId() != null) {
            customerWrapper.like(Customer::getId, businessPageQueryDTO.getId());
        }
        if (businessPageQueryDTO.getName() != null) {
            customerWrapper.like(Customer::getName, businessPageQueryDTO.getName());
        }
        if (businessPageQueryDTO.getPhone() != null) {
            customerWrapper.like(Customer::getPhone, businessPageQueryDTO.getPhone());
        }
        if (businessPageQueryDTO.getBeginCreateTime() != null && businessPageQueryDTO.getEndCreateTime() != null) {
            customerWrapper.between(Customer::getCreateTime, businessPageQueryDTO.getBeginCreateTime(), businessPageQueryDTO.getEndCreateTime());
        }

        // 封装pageResult
        Page<Customer> customerPage = customerService.page(new Page<>(businessPageQueryDTO.getPageNum(), businessPageQueryDTO.getPageSize()), customerWrapper);
        List<Customer> records = customerPage.getRecords();
        if (!records.isEmpty()) {
            records.forEach(customer -> {
                BusinessPageQueryVO businessPageQueryVO = new BusinessPageQueryVO();
                BeanUtil.copyProperties(customer, businessPageQueryVO);
                VOList.add(businessPageQueryVO);
            });
        }
        PageResult pageResult = new PageResult(VOList.size(), VOList);
        return pageResult;
    }

    /**
     * 新增商机
     * @param businessPageQueryVO
     */
    @Override
    public void saveBusiness(BusinessPageQueryVO businessPageQueryVO) {
        // 先保存客户信息，再保存商机信息
        Customer customer = new Customer();
        BeanUtil.copyProperties(businessPageQueryVO, customer);
        customerService.save(customer);
        Business business = new Business();
        BeanUtil.copyProperties(businessPageQueryVO, business);
        //依据保存后回填生成的客户唯一id将客户和商机关联
        business.setCustomerId(customer.getId());
        save(business);
    }

    /**
     * 商机分配
     * @param requestData
     */
    @Override
    //todo :要与人员表进行联查判断，判断被指派人有多少商机正在跟进
    public void assignBusiness(RequestData requestData) {
        //判断商机是否达到保有量上限
        //判断用户是否达到最大商机量上限
    }

    @Override
    public Customer getByCustomerInfoId(Long id) {
        //依据商机id查询到相应的客户id，进而查询到客户信息
        Business business = getById(id);
        if (business == null){
            log.error("商机不存在");
            return null;
        }
        return customerService.getById(business.getCustomerId());
    }
}
